package com.example.administrator.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;

public class JsonActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_json);
        ButterKnife.bind(this);
    }
    @OnClick({R.id.bt_json1,R.id.bt_json2,R.id.bt_json3,R.id.bt_gson1,R.id.bt_gson2,R.id.bt_gson3})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.bt_json1:
                String jsonStr = "{'data':{'name':'张三','pwd':'123456'},'msg':'操作 成功','status':0}";
                try {
                    JSONObject jsonObject = new JSONObject(jsonStr);
                    String msg = jsonObject.getString("msg");
                    int  status = jsonObject.getInt("status");
                    JSONObject dataObj = jsonObject.getJSONObject("data");
                    String name = dataObj.getString("name");
                    String pwd = dataObj.getString("pwd");
                    Log.i("Json：", msg + " " + status + " " + name + " " + pwd);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.bt_json2:
                String jsonStr2 = "{'data':[{'name':'张三','pwd':'123456'},{'name':'李四','pwd':'222222'},{'name':'王五','pwd':'999999999999999'}],'msg':'操作 成功','status':0}";
                try {
                    JSONObject jsonObject = new JSONObject(jsonStr2);
                    JSONArray dataArray = jsonObject.getJSONArray("data");
                    StringBuffer sb = new StringBuffer();

                    for (int i = 0; i < dataArray.length(); i++) {
                        JSONObject dataObj = dataArray.getJSONObject(i);
                        String name = dataObj.getString("name");
                        String pwd = dataObj.getString("pwd");
                        sb.append(name).append(pwd);
                    }

                    String msg = jsonObject.getString("msg");
                    int status = jsonObject.getInt("status");
                    sb.append(msg).append(status);

                    Log.i("Json", sb.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.bt_json3:
                String jsonStr3 = "{'message':'OK','status':0,'data':[{'summary':'这种与巨头间的平衡关系在财报上也有体现。" +
                        "猎豹的广告收入中有70%来自BAT三家公司，总收入中有58%来自BAT。从上述来看，雷军在对金山改组、选将、外交上" +
                        "的智慧，为猎豹的发展铺下了坚实的路。','icon':'http:\\/\\/192.168.1.168:80\\/Images\\/2014050909082" +
                        "044669.png','stamp':'2016-04-25 16:13:44.0','title':'写在猎豹上市：起承转合这几年','nid':4,'link'" +
                        ":'http:\\/\\/tech.163.com\\/14\\/0509\\/09\\/9RPSM3TJ000915BF.html','type':1},{'summary':'如果说视" +
                        "频是一座江湖，而方式代表了两大武功的流派。有机增长即潜心修炼打通任督二脉，武功大成，缺点是太慢；战略组合则是" +
                        "吸取他人功力化为己用，速度快但问题在于内力容易冲突。','icon':'http:\\/\\/192.168.1.168:80\\/Images\\/20" +
                        "1405090904139ebf2.png','stamp':'2016-04-25 16:13:44.0','title':'视频行业：吸星大法还是打通任督二脉','nid" +
                        "':5,'link':'http:\\/\\/tech.163.com\\/14\\/0509\\/08\\/9RPS4ECE000915BF.html','type':1},{'summary':'4" +
                        "月18日，海蝶音乐创始人许环良正式宣布自己辞去CEO的职务，离开了亲手创办的唱片公司，就在国内资本蠢蠢欲动、外资巨鳄" +
                        "觊觎内地市场的时节，唱片公司老板转身的原因是什么？','icon':'http:\\/\\/192.168.1.168:80\\/Images\\/2014050" +
                        "9075810c679d.png','stamp':'2016-04-25 16:13:44.0','title':'音乐生态乱象 是互联网的错么？','nid':6,'link':" +
                        "'http:\\/\\/tech.163.com\\/14\\/0509\\/06\\/9RPKCLTM00094ODU.html','type':1}]}";
                try {
                    StringBuffer sb = new StringBuffer();
                    JSONObject jsonObject = new JSONObject(jsonStr3);
                    String message = jsonObject.getString("message");
                    int status = jsonObject.getInt("status");
                    sb.append(message).append(status).append("\n");
                    JSONArray dataArray = jsonObject.getJSONArray("data");
                    for (int i = 0; i < dataArray.length(); i++) {
                        JSONObject dataObj = dataArray.getJSONObject(i);
                        String summary = dataObj.getString("summary");
                        String icon = dataObj.getString("icon");
                        String stamp = dataObj.getString("stamp");
                        String title = dataObj.getString("title");
                        int nid = dataObj.getInt("nid");
                        String link = dataObj.getString("link");
                        int type = dataObj.getInt("type");

                        sb.append(summary).append(" ").append(icon).append(" ").append(stamp)
                                .append(title).append(nid).append(link).append(type).append("\n");

                    }
                    Log.i("Json",sb.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.bt_gson1:
                String jsonStr4 = "{'message':'OK','status':0,'data':[{'summary':'这种与巨头间的平衡关系在财报上也有体现。" +
                        "猎豹的广告收入中有70%来自BAT三家公司，总收入中有58%来自BAT。从上述来看，雷军在对金山改组、选将、外交上" +
                        "的智慧，为猎豹的发展铺下了坚实的路。','icon':'http:\\/\\/192.168.1.168:80\\/Images\\/2014050909082" +
                        "044669.png','stamp':'2016-04-25 16:13:44.0','title':'写在猎豹上市：起承转合这几年','nid':4,'link'" +
                        ":'http:\\/\\/tech.163.com\\/14\\/0509\\/09\\/9RPSM3TJ000915BF.html','type':1},{'summary':'如果说视" +
                        "频是一座江湖，而方式代表了两大武功的流派。有机增长即潜心修炼打通任督二脉，武功大成，缺点是太慢；战略组合则是" +
                        "吸取他人功力化为己用，速度快但问题在于内力容易冲突。','icon':'http:\\/\\/192.168.1.168:80\\/Images\\/20" +
                        "1405090904139ebf2.png','stamp':'2016-04-25 16:13:44.0','title':'视频行业：吸星大法还是打通任督二脉','nid" +
                        "':5,'link':'http:\\/\\/tech.163.com\\/14\\/0509\\/08\\/9RPS4ECE000915BF.html','type':1},{'summary':'4" +
                        "月18日，海蝶音乐创始人许环良正式宣布自己辞去CEO的职务，离开了亲手创办的唱片公司，就在国内资本蠢蠢欲动、外资巨鳄" +
                        "觊觎内地市场的时节，唱片公司老板转身的原因是什么？','icon':'http:\\/\\/192.168.1.168:80\\/Images\\/2014050" +
                        "9075810c679d.png','stamp':'2016-04-25 16:13:44.0','title':'音乐生态乱象 是互联网的错么？','nid':6,'link':" +
                        "'http:\\/\\/tech.163.com\\/14\\/0509\\/06\\/9RPKCLTM00094ODU.html','type':1}]}";
                Gson gson = GsonUtils.getGson();
                News news = gson.fromJson(jsonStr4,News.class);
                Log.i("Json",news.toString());
                break;
            case R.id.bt_gson2:
                String jsonStr5 = "{'data':[{'name':'张三','pwd':'123456'},{'name':'李四','pwd':'222222'},{'name':'王五','pwd':'999999999999999'}],'msg':'操作 成功','status':0}";
                Person2 person2 = GsonUtils.getGson().fromJson(jsonStr5,Person2.class);
                Log.i("Json",person2.toString());
                break;
            //数组
            case R.id.bt_gson3:
                String jsonStr6 = "[{'name':'张三','pwd':'123456'},{'name':'李四','pwd':'222222'},{'name':'王五','pwd':'999999999999999'}]";
                Type type = new TypeToken<List<Person2.Data>>(){}.getType();
                List<Person2.Data> list = GsonUtils.getGson().fromJson(jsonStr6, type);
                Log.i("Json",list.toString());
                break;
        }
    }
}
